#include #include // for rand #include #include #include #include using namespace std; // create a function object // this is not a function but looks like one. // it differs from a function in that data // is persistant. struct sumk{ int total; sumk():total(0){}// constructor void operator()(int i){total+=i;}//overload (x) operator }; void main(){ // declare a container to hold ints vector v; // load the container with random #'s srand(time(0)); for(int i = 0; i < 5; i++) v.push_back(rand()%10+1); // show original cout <<"Original Data"<::iterator it;// declare iterator variable for(it = v.begin(); it != v.end(); it++)// must use != cout << *it << endl; // "dereference" the iterator // use the sort algorithm sort(v.begin(), v.end()); // do an O(log(n)) search cout << "search for 1-10 yields " << endl; for(int i = 0; i < 10; i++) cout << binary_search(v.begin(), v.end(), i+1); cout << endl; // show results cout <<"Sorted Data"< out(cout, "\n"); copy(v.begin(), v.end(), out); // backwards the wrong way cout << "incorrect print reverse sorted data" << endl; copy(v.begin(), v.end(), out); // backwards the correct way cout << "correct print reverse sorted data" << endl; vector::reverse_iterator ri; for(ri = v.rbegin(); ri != v.rend(); ri++) cout << *ri << endl; // use random shuffle algorithm cout <<"After shuffle"< k; for(int i = 0; i < 5; i++) k.push_back(rand()%10+1); cout << "list data" << endl; list::iterator ik; copy(k.begin(), k.end(), out); // report on sum of list ints cout << "sum of list ints " << endl; cout << (for_each(k.begin(), k.end(), sumk())).total << endl; // this notation is exceptional in the STL k.sort(); // show result of sort cout<<"sorted linked list"<